<template>
    <div>
        <el-button type="primary" @click="dialogVisible = true">添加用户</el-button>
        <el-table :data="tableData" style="width: 100%">
            <el-table-column label="角色名称" width="180">
                <template #default="scope">
                    <div>{{ scope.row.role.roleName }}</div>
                </template>
            </el-table-column>
            <el-table-column prop="username" label="用户名" width="180" />
            <el-table-column label="操作">
                <template #default="scope">
                    <div style="display: flex; align-items: center">
                        <el-button type="primary" round @click="handleUpdata(scope.row)"
                            :disabled="scope.row.default">编辑</el-button>
                        <el-popconfirm title="确定要删除吗?" confirm-button-text="确定" cancel-button-text="取消"
                            @confirm="handleDelete(scope.row)">
                            <template #reference>
                                <el-button type="danger" round :disabled="scope.row.default">删除</el-button>
                            </template>
                        </el-popconfirm>
                    </div>
                </template>
            </el-table-column>
        </el-table>

        <!-- 添加用户 -->
        <el-dialog v-model="dialogVisible" title="添加用户">
            <el-form :model="addForm" status-icon label-width="80">
                <el-form-item label="用户名称" prop="username">
                    <el-input v-model="addForm.username" />
                </el-form-item>
                <el-form-item label="密码" prop="password">
                    <el-input v-model="addForm.password" type="password" />
                </el-form-item>
                <el-form-item label="角色" prop="role">
                    <el-select v-model="addForm.role" class="m-2" placeholder="请选择角色" size="large" style="width:100%">
                        <el-option v-for="item in rolelist" :key="item._id" :label="item.roleName" :value="item._id" />
                    </el-select>
                </el-form-item>

            </el-form>

            <template #footer>
                <span class="dialog-footer">
                    <el-button @click="dialogVisible = false">取消</el-button>
                    <el-button type="primary" @click="handleAddConfirm()">
                        确定
                    </el-button>
                </span>
            </template>
        </el-dialog>
        <!-- 更新/编辑用户 -->
        <el-dialog v-model="updataDialogVisible" title="编辑用户">
            <el-form :model="updateForm" status-icon label-width="80">
                <el-form-item label="用户名称" prop="username">
                    <el-input v-model="updateForm.username" />
                </el-form-item>
                <el-form-item label="密码" prop="password">
                    <el-input v-model="updateForm.password" type="password" />
                </el-form-item>
                <el-form-item label="角色" prop="role">
                    <el-select v-model="updateForm.role" class="m-2" placeholder="请选择角色" size="large" style="width:100%">
                        <el-option v-for="item in rolelist" :key="item._id" :label="item.roleName" :value="item._id" />
                    </el-select>
                </el-form-item>

            </el-form>

            <template #footer>
                <span class="dialog-footer">
                    <el-button @click="updataDialogVisible = false">取消</el-button>
                    <el-button type="primary" @click="handleUpdataConfirm()">
                        确定
                    </el-button>
                </span>
            </template>
        </el-dialog>
    </div>
</template>

<script setup>
import { ref, onMounted, reactive } from 'vue'
import axios from 'axios'
const tableData = ref([])
const rolelist = ref([])
onMounted(async () => {
    getList()
    getRoleList()
})

const getList = async () => {
    let res = await axios.get('/adminapi/users')
    tableData.value = res.data
}
// 获取角色列表
const getRoleList = async () => {
    let res = await axios.get('/adminapi/roles')
    rolelist.value = res.data
}
// 添加用户
const dialogVisible = ref(false)
const addForm = reactive({
    username: '',
    password: '',
    role: ''
})
// add确定
const handleAddConfirm = async () => {
    console.log(addForm)
    dialogVisible.value = false
    // 添加用户数据
    await axios.post('/adminapi/users', addForm)
    await getList()
    addForm.password = ''
    addForm.username = ''
    addForm.role = ''
}


// 编辑用户
const updateForm = reactive({
    username: '',
    password: '',
    role: ''
})
const updataDialogVisible = ref(false)
const currentItem = ref({})
const handleUpdata = (item) => {
    updataDialogVisible.value = true
    console.log(item)
    updateForm._id = item._id
    updateForm.username = item.username
    updateForm.password = item.password
    updateForm.role = item.role._id
    currentItem.value = item
}

// 编辑确定
const handleUpdataConfirm = async () => {
    updataDialogVisible.value = false
    // // 编辑用户数据
    await axios.put(`/adminapi/users/${currentItem.value._id}`, updateForm)
    await getList()
    updateForm.password = ''
    updateForm.username = ''
    updateForm.role = ''
}

// 删除用户
const handleDelete = async (item) => {
    await axios.delete(`/adminapi/users/${item._id}`)
    await getList()
}
</script>